package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;

    public class AudioNavShell extends Sprite
    {
        public var myXMLList:XMLList;
        var counter:int;
        var nav:ProjPaginatedMenu;
        var fadeSpeed:Number;
        var listLength:int;
        var currImageId:int;
        var currNavId:int;
        var myRoot:MovieClip;
        var audioShell:AudioShell;

        public function AudioNavShell(param1:MovieClip, param2:Number, param3:XMLList) : void
        {
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            listLength = myXMLList.length();
            counter = 0;
            currNavId = 0;
            nav = new ProjPaginatedMenu(myRoot, fadeSpeed, listLength);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            addChild(nav);
            updateAudio();
            return;
        }

        function switchAudio() : void
        {
            audioShell.killAudio();
            TweenMax.to(audioShell, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                removeAudioShell();
                loadAudio(myXMLList[currNavId].@id, myXMLList[currNavId].@url, myXMLList[currNavId].longTitle);
                return;
            }
            });
            return;
        }

        public function fadeOutShell() : void
        {
            if (audioShell != null)
            {
                audioShell.killAudio();
            }
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                removeAudioShell();
                return;
            }
            });
            return;
        }

        function doResize() : void
        {
            nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick) : void
        {
            currNavId = param1.currentTarget.currNavId;
            updateAudio();
            return;
        }

        function loadAudio(param1:int, param2:String, param3:String) : void
        {
            audioShell = new AudioShell(myRoot, fadeSpeed);
            addChild(audioShell);
            audioShell.loadAudio(param2, param3);
            return;
        }

        function removeAudioShell() : void
        {
            if (audioShell != null)
            {
                removeChild(audioShell);
                audioShell = null;
            }
            return;
        }

        function updateAudio() : void
        {
            if (audioShell == null)
            {
                loadAudio(myXMLList[currNavId].@id, myXMLList[currNavId].@url, myXMLList[currNavId].longTitle);
            }
            else
            {
                switchAudio();
            }
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true, onComplete:function ()
            {
                if (audioShell == null)
                {
                    loadAudio(myXMLList[currNavId].@id, myXMLList[currNavId].@url, myXMLList[currNavId].longTitle);
                }
                return;
            }
            });
            return;
        }

    }
}
